class Solution {
public:
    int maxProfit(vector<int> &prices)
    {
        int size = prices.size();
        if(size < 2) {
            return 0;
        }
        vector<int> f(size, 0);
        vector<int> g(size, 0);

        for(int i = 1, low = prices[0]; i < size; ++i) {
            low = min(prices[i], low);
            f[i] = max(f[i - 1], prices[i] - low);
        }

        for(int i = size - 2, high = prices[size - 1]; i >= 0; i--) {
            high = max(high, prices[i]);
            g[i] = max(g[i], high - prices[i]);
        }

        int MaxV = -1;
        for(int i = 0; i < size; ++i) {
            MaxV = max(MaxV, f[i] + g[i]);
        }
        return MaxV;
    }
};